package arrays;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringJoiner;

public class P4 {

    public static void main(String[] args) {
        int[] nums = new int[] { 3, -2, 1, 0 };
        List<List<Integer>> ints = threeSum(nums);
        System.out.println(ints);
    }

    public static List<List<Integer>> threeSum(int[] nums) {
        if (nums.length < 3) {
            return new ArrayList<>(0);
        }
        Set<Integer> set = new HashSet<>();
        for (int it : nums) {
            set.add(it);
        }
        List<Integer> list = new ArrayList<>(set);
        list.sort(Integer::compare);
        if (list.get(0) >= 0 || list.get(list.size() - 1) <= 0) {
            return new ArrayList<>(0);
        }
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            map.put(list.get(i), i);
        }
        int len = list.size();
        List<List<Integer>> result = new ArrayList<>();
        for (int i = 0; list.get(i) <= 0; i++) {
            for (int j = len - 1; list.get(j) > 0; j++) {
                int sum = list.get(i) + list.get(j);
                int key = 0 - sum;
                if (map.containsKey(key)) {
                    int index = map.get(key);
                    if (index < j && index > i) {
                        List<Integer> item = new ArrayList<>();
                        item.add(list.get(i));
                        item.add(list.get(j));
                        item.add(key);
                        result.add(item);
                    }
                }
            }
        }

        return null;
    }
}
